package com.situ.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.ssm.common.ServerResponse;
import com.situ.ssm.dao.StudentMapper;
import com.situ.ssm.entity.Student;
import com.situ.ssm.service.IStudentService;

@Service
public class StudentServiceImpl implements IStudentService{

	@Autowired
	private StudentMapper studentMapper;

	@Override
	public ServerResponse pageList(Integer page, Integer limit,Student student) {
		PageHelper.startPage(page, limit);
		List<Student> list = studentMapper.pageList(student);
		PageInfo pageInfo = new PageInfo(list);
		Integer count = (int) pageInfo.getTotal();

		return ServerResponse.createSuccess("", count, list);
	}

	@Override
	public ServerResponse deleteById(Integer id) {
		int count = studentMapper.deleteById(id);
		if(count != 1) {
			return ServerResponse.createError("删除失败");
		}
		return ServerResponse.createSuccess("删除成功");
	}

	@Override
	public ServerResponse deleteAll(Integer[] ids) {
		int count = studentMapper.deleteAll(ids);
		if(count != ids.length) {
			return ServerResponse.createError("删除失败");
		}
		return ServerResponse.createSuccess("删除成功");
	}

	@Override
	public ServerResponse insert(Student student) {
		int count = studentMapper.insert(student);
		if (count != 1) {
			return ServerResponse.createError("添加失败");
		}
		
		return ServerResponse.createSuccess("添加成功");
	}

	@Override
	public Student selectById(Integer id) {
		Student student = studentMapper.selectById(id);
		return student;
	}

	@Override
	public ServerResponse updateById(Student student) {
		int count = studentMapper.updateById(student);
		if (count != 1) {
			return ServerResponse.createError("更新失败");
		}
		
		return ServerResponse.createSuccess("更新成功");
	}
	
	
	

	
}
